package com.woniu.aimd.controller;

import cn.hutool.core.lang.UUID;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class ImageUploadController {

    @PostMapping("/image")
    public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }

        // 保存路径，例如：D:/uploads/
        String uploadDir = "D:/uploads/";
        String originalFilename = file.getOriginalFilename();
        String newFilename = UUID.randomUUID() + "_" + originalFilename;

        File saveFile = new File(uploadDir + newFilename);
        file.transferTo(saveFile);

        // 构造返回的图片 URL（假设通过 Spring 静态资源暴露）
        String imageUrl = "/uploads/" + newFilename;

        Map<String, Object> result = new HashMap<>();
        result.put("url", imageUrl);
        return result;
    }


}
